package ru.agiledev.aos.frontend.client.panel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import ru.agiledev.aos.frontend.client.AspectOrientedSearchApplication;
import ru.agiledev.aos.frontend.shared.SearchResult;
import ru.agiledev.aos.frontend.shared.TransformationResult;

import java.util.List;

/**
 * Created by Anton Kirillov
 * Date: 8/10/11
 */
public class ResultPanel extends VerticalPanel{

    private SearchResultsPanel searchResultsPanel;
    private HorizontalPanel yandexBanner;
    private HTML yResults = new HTML();


    public ResultPanel() {
        setStyleName("resultsPanel");

        yResults.setStyleName("yandexStats");
        yandexBanner = new HorizontalPanel();
        yandexBanner.setStyleName("yandexBanner");
        Image banner = new Image("img/yandex.png");
        banner.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                Window.open("http://www.yandex.ru","target","");
            }
        });
        banner.setStyleName("yandexLogo");

        yandexBanner.add(banner);
        yandexBanner.add(yResults);
        yResults.setHTML("Поиск реализован с использованием <a href=\"http://xml.yandex.ru/\" target=\"_blank\">Яндекс.XML</a>");


        searchResultsPanel = new SearchResultsPanel();

        add(yandexBanner);
        add(searchResultsPanel);
    }

    public void setTransformationResults(TransformationResult result){
        InfoPanel info = AspectOrientedSearchApplication.get().getInfoPanel();
        info.setResults(result);
    }

    public void setResponse(List<SearchResult> results){
        searchResultsPanel.setContent(results);
    }

    public void clearTransformationResults(){
        InfoPanel info = AspectOrientedSearchApplication.get().getInfoPanel();
        info.clearResults();
    }



    class SearchResultsPanel extends VerticalPanel{
        public void setContent(List<SearchResult> results){
            clear();
                for(SearchResult result: results){
                    add(new ResultEntryPanel(result));
                }
        }


    }
}
